<?php

// @formatter:off
/**
 * @file WCFVMenu.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: WCFVMenu.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/includes/WCFVMenu.php $
 */
// @formatter:on

class WCFVMenuNode {
	//
	// Protected properties.
	protected $_cssClass = false;
	protected $_ficon = false;
	protected $_icon = false;
	protected $_name = false;
	protected $_title = false;
	protected $_url = false;
	//
	// Magic methods.
	public function __construct($name) {
		$this->setName($name);
		$this->setTitle($name);
	}
	//
	// Public methods.
	public function cssClass() {
		return $this->_cssClass;
	}
	public function ficon() {
		return $this->_ficon;
	}
	public function icon() {
		return $this->_icon;
	}
	public function name() {
		return $this->_name;
	}
	public function setCssClass($cssClass) {
		$this->_cssClass = $cssClass;
		return $this->cssClass();
	}
	public function setFicon($ficon) {
		$this->_ficon = $ficon;
		return $this->ficon();
	}
	public function setFromArray(array $list) {
		if(isset($list["class"])) {
			$this->setCssClass($list["class"]);
		}
		if(isset($list["ficon"])) {
			$this->setFicon($list["ficon"]);
		}
		if(isset($list["icon"])) {
			$this->setIcon($list["icon"]);
		}
		if(isset($list["name"])) {
			$this->setName($list["name"]);
		}
		if(isset($list["title"])) {
			$this->setTitle($list["title"]);
		}
		if(isset($list["url"])) {
			$this->setUrl($list["url"]);
		}

		return $this->toItem();
	}
	public function setIcon($icon) {
		$this->_icon = $icon;
		return $this->icon();
	}
	public function setName($name) {
		$this->_name = $name;
		return $this->name();
	}
	public function setTitle($title) {
		$this->_title = $title;
		return $this->title();
	}
	public function setUrl($url) {
		$this->_url = $url;
		return $this->url();
	}
	public function toItem() {
		$out = array();

		$out["class"] = $this->cssClass();
		$out["ficon"] = $this->ficon();
		$out["icon"] = $this->icon();
		$out["name"] = $this->name();
		$out["title"] = $this->title();
		$out["url"] = $this->url();

		return array_filter($out);
	}
	public function title() {
		if(!$this->_title) {
			$this->_title = $this->name();
		}
		return $this->_title;
	}
	public function url() {
		return $this->_url;
	}
}

class WCFVMenu extends WCFactory {
	//
	// Public class methods.
	public static function GetMenuItems($code, $toItem = true) {
		$items = array();

		self::TriggerMenuBuilders($code);

		switch($code) {
			case WC_MENU_CODE_MAIN:
				$items = self::GetMainMenuItems();
				break;
			case WC_MENU_CODE_SHORTCUTS:
				$items = self::GetShortcutsMenuItems();
				break;
			case WC_MENU_CODE_USER:
				$items = self::GetUserMenuItems();
				break;
			case WC_MENU_CODE_ADMIN:
				$items = self::GetAdminMenuItems();
				break;
			default:
				// @todo extramenut
				break;
		}

		if($toItem) {
			$aux = array();
			foreach($items as $item) {
				$aux[] = $item->toItem();
			}
			$items = $aux;
		}

		return $items;
	}
	//
	// Protected class methods.
	protected static function GetAdminMenuItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcUser;
		global $wcMenuItems;

		$lang = WCTranslate::I();

		if($wcUser && $wcUser->ok() && WCProfile::HasProfile($wcUser, "ADMIN", $wcGroup)) {
			foreach($wcMenuItems[WC_MENU_CODE_ADMIN]["top"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}

			if($wcProfile->allowedTo(WC_PERM_USER_EDIT)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_users"));
				$aux->setTitle($lang->translate("menu_title_users"));
				$aux->setIcon("user-icon.png");
				$aux->setUrl("?action=".WC_ACTION_USER_LIST."&group={$wcGroup->id()}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_CACHE_EDIT)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_cache"));
				$aux->setTitle($lang->translate("menu_title_cache"));
				$aux->setIcon("cache-icon.png");
				$aux->setUrl("?action=".WC_ACTION_CACHE."&group={$wcGroup->id()}");
				$items[] = $aux;
			}

			foreach($wcMenuItems[WC_MENU_CODE_ADMIN]["middle"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}

			if($wcProfile->allowedTo(WC_PERM_USER_EDIT)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_stats"));
				$aux->setTitle($lang->translate("menu_title_stats"));
				$aux->setIcon("stats-icon.png");
//				$aux->setUrl("?action=".WC_ACTION_COLLECTION_LIST."&group={$groupId}");
				$items[] = $aux;
			}

			foreach($wcMenuItems[WC_MENU_CODE_ADMIN]["bottom"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}
		} else {
			$aux = new WCFVMenuNode("Insufficient Privileges");
			$aux->setTitle("Insufficient Privileges, please login");
			$aux->setIcon("admin-icon.png");
			$aux->setUrl("?group={$wcGroup->id}");

			$items[] = $aux;
		}

		return $items;
	}
	protected static function GetMainMenuItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcDefaults;
		global $wcMenuItems;

		$lang = WCTranslate::I();
		$groupId = $wcGroup->id;

		if($wcProfile->grantedGroup($wcGroup)) {
			foreach($wcMenuItems[WC_MENU_CODE_MAIN]["top"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}

			$aux = new WCFVMenuNode($lang->translate("menu_label_home"));
			$aux->setTitle($lang->translate("menu_title_home"));
			$aux->setIcon("home-icon.png");
			$aux->setUrl("?group={$groupId}");
			$items[] = $aux;

			if($wcProfile->allowedTo(WC_PERM_COLLECTION_LIST)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_collections"));
				$aux->setTitle($lang->translate("menu_title_collections"));
				$aux->setIcon("collections-icon.png");
				$aux->setUrl("?action=".WC_ACTION_COLLECTION_LIST."&group={$groupId}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_tags"));
				$aux->setTitle($lang->translate("menu_title_tags"));
				$aux->setIcon("tags-icon.png");
				$aux->setUrl("?action=".WC_ACTION_TAG_LIST."&group={$groupId}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_DIRECTORY_LIST)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_directories"));
				$aux->setTitle($lang->translate("menu_title_directories"));
				$aux->setIcon("directories-icon.png");
				$aux->setUrl("?action=".WC_ACTION_DIRECTORY_LIST."&group={$groupId}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_SPECIAL_SEARCH)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_search"));
				$aux->setTitle($lang->translate("menu_title_search"));
				$aux->setIcon("search-icon.png");
				$aux->setUrl("?action=".WC_ACTION_SEARCH."&group={$groupId}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_RULE_LIST)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_rules"));
				$aux->setTitle($lang->translate("menu_title_rules"));
				$aux->setIcon("rules-icon.png");
				$aux->setUrl("?action=".WC_ACTION_RULE_LIST."&group={$groupId}");
				$items[] = $aux;
			}
			if($wcProfile->allowedTo(WC_PERM_SPECIAL_STATS)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_stats"));
				$aux->setTitle($lang->translate("menu_title_stats"));
				$aux->setIcon("stats-icon.png");
				$aux->setUrl("?action=".WC_ACTION_STATS."&group={$groupId}");
				$items[] = $aux;
			}
//			if($wcProfile->allowedTo(WC_PERM_USER_LIST)) {
//				$aux = new WCFVMenuNode($lang->translate("menu_label_users"));
//				$aux->setTitle($lang->translate("menu_title_users"));
//				$aux->setIcon("user-icon.png");
//				$aux->setUrl("?action=".WC_ACTION_USER_LIST."&group={$groupId}");
//				$items[] = $aux;
//			}

			foreach($wcMenuItems[WC_MENU_CODE_MAIN]["middle"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}

			if(!$wcDefaults["no-credits"] && $wcProfile->allowedTo(WC_PERM_SPECIAL_CREDIT)) {
				$aux = new WCFVMenuNode($lang->translate("menu_label_credits"));
				$aux->setTitle($lang->translate("menu_title_credits"));
				$aux->setIcon("wcomix.png");
				$aux->setUrl("?action=".WC_ACTION_CREDITS."&group={$groupId}");
				$items[] = $aux;
			}

			foreach($wcMenuItems[WC_MENU_CODE_MAIN]["bottom"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}
		} else {
			$aux = new WCFVMenuNode("Insufficient Privileges");
			$aux->setTitle("Insufficient Privileges, please login");
			$aux->setIcon("mainmenu-icon.png");
			$aux->setUrl("?group={$wcGroup->id}");

			$items[] = $aux;
		}

		return $items;
	}
	protected static function GetShortcutsMenuItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcMenuItems;

		if($wcProfile->grantedGroup($wcGroup)) {
			foreach($wcMenuItems[WC_MENU_CODE_SHORTCUTS]["top"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}

			$tags = array();
			foreach(WCActiveTagsHolder::I()->tags($wcGroup) as $name => $activeTag) {
				if($activeTag->ok() && ($activeTag->tag()->count || $activeTag->tag()->icount)) {
					$tags[$name] = $activeTag->tag();
				}
			}
			ksort($tags);

			foreach($tags as $tag) {
				$aux = new WCFVMenuNode($tag->name);
				$aux->setTitle("Tag '{$tag->name}'");
				$aux->setIcon("tags-icon.png");
				$aux->setUrl("?action=content:tag&group={$wcGroup->id}&tag={$tag->id}");

				$items[] = $aux;
			}

			foreach($wcMenuItems[WC_MENU_CODE_SHORTCUTS]["bottom"] as $item) {
				$aux = new WCFVMenuNode("unnamed");
				$aux->setFromArray($item);
				$items[] = $aux;
			}
		} else {
			$aux = new WCFVMenuNode("Insufficient Privileges");
			$aux->setTitle("Insufficient Privileges, please login");
			$aux->setIcon("tags-icon.png");
			$aux->setUrl("?group={$wcGroup->id}");

			$items[] = $aux;
		}


		return $items;
	}
	protected static function GetUserMenuItems() {
		$items = array();
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcGroup;
		global $wcSession;
		global $wcUser;
		global $wcMenuItems;

		foreach($wcMenuItems[WC_MENU_CODE_USER]["top"] as $item) {
			$aux = new WCFVMenuNode("unnamed");
			$aux->setFromArray($item);
			$items[] = $aux;
		}

		$lang = WCTranslate::I();
		$groupId = $wcGroup->id;
		//
		// Checking if the group switching system should be activated.
		$groupsCount = 0;
		foreach(WCGroupsHolder::I()->items() as $group) {
			if($wcProfile->grantedGroup($group) && is_readable($group->root)) {
				$groupsCount++;
			}
		}
		if($groupsCount > 1) {
			$aux = new WCFVMenuNode($lang->translate("menu_label_change_group"));
			$aux->setTitle($lang->translate("menu_title_change_group"));
			$aux->setIcon("home-icon.png");
			$aux->setCssClass("wc-usermenu-switchgroup");
			$items[] = $aux;
		}

		if(defined("__IS_MOBILE__")) {
			$status = defined("__MOBILE__");
			$aux = new WCFVMenuNode($lang->translate("menu_label_mobile_mode_".($status ? "off" : "on")));
			$aux->setTitle($lang->translate("menu_title_mobile_mode_".($status ? "off" : "on")));
			$aux->setIcon("mobile-icon.png");
			$aux->setCssClass("wc-usermenu-mobile");
			$items[] = $aux;
		}

		foreach($wcMenuItems[WC_MENU_CODE_USER]["middle"] as $item) {
			$aux = new WCFVMenuNode("unnamed");
			$aux->setFromArray($item);
			$items[] = $aux;
		}

		if($wcSession->isLogged()) {
			$aux = new WCFVMenuNode($lang->translate("menu_label_profile"));
			$aux->setTitle($lang->translate("menu_title_profile"));
			$aux->setFicon(WCViewManager::I()->USER_AVATAR);
			$aux->setUrl("?action=".WC_ACTION_USER_EDIT."&group={$groupId}&user={$wcUser->id()}");
			$items[] = $aux;

			$aux = new WCFVMenuNode($lang->translate("menu_label_log_out"));
			$aux->setTitle($lang->translate("menu_title_log_out"));
			$aux->setIcon("no-user-icon.png");
			$aux->setCssClass("wc-usermenu-logout");
			$items[] = $aux;
		} else {
			$aux = new WCFVMenuNode($lang->translate("menu_label_log_in"));
			$aux->setTitle($lang->translate("menu_title_log_in"));
			$aux->setIcon("user-icon.png");
			$aux->setCssClass("wc-usermenu-login");
			$items[] = $aux;

			$aux = new WCFVMenuNode($lang->translate("menu_label_sign_up"));
			$aux->setTitle($lang->translate("menu_title_sign_up"));
			$aux->setIcon("user-icon.png");
			$aux->setUrl("?action=".WC_ACTION_USER_REGISTER."&group={$groupId}");
			$items[] = $aux;
		}

		foreach($wcMenuItems[WC_MENU_CODE_USER]["top"] as $item) {
			$aux = new WCFVMenuNode("unnamed");
			$aux->setFromArray($item);
			$items[] = $aux;
		}

		return $items;
	}
	protected static function TriggerMenuBuilders($code) {
		global $wcMenuBuilders;

		foreach($wcMenuBuilders as $builder) {
			$builder($code);
		}
	}
}
